package mv.crawler;

import mv.wap.Page;
import mv.main.MObject;

import javax.swing.text.MutableAttributeSet;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * Class description...
 *
 * @author mvu
 */
public class HtmlCrawler extends MObject {
  List<Page> pages = new ArrayList<Page>();

  public List<Page> crawl() throws Exception {

    StringBuffer html = new StringBuffer();

    HTMLEditorKit.ParserCallback callback = new CallBack();
    Reader reader = new InputStreamReader(new URL("http://vnexpress.net/GL/Home/").openStream());
    ParserDelegator delegator = new ParserDelegator();
    delegator.parse(reader, callback, true);

    //pages.add(new Page("alankeefer", html.toString()));
    return pages;
  }

  class CallBack extends HTMLEditorKit.ParserCallback {
    Stack<HTML.Tag> stack = new Stack<HTML.Tag>();
    StringBuffer br;

    @Override
    public void handleStartTag(HTML.Tag t, MutableAttributeSet a, int pos) {
      if(br == null){
        if(t.toString().equals("div") && a.containsAttribute(HTML.Attribute.CLASS, "hotnews-detail fl")){
          br = new StringBuffer();
          print(a.getAttribute(HTML.Attribute.CLASS));
        }
      }else{
        stack.push(t);
        br.append("<").append(t.toString()).append(">");
      }
    }

    @Override
    public void handleEndTag(HTML.Tag t, int pos) {
      if(br != null){
        if(stack.isEmpty()){
          print(br.toString());
        }else{
          stack.pop();
          br.append("</").append(t.toString()).append(">");
        }
      }
    }

    @Override
    public void handleText(char[] data, int pos) {
      if(br != null){
        br.append(String.copyValueOf(data));
      }
    }
  }

  

  public static void main(String[] args) throws Exception {
    new HtmlCrawler().crawl();
  }
}
